import { defineConfig, loadEnv } from 'vite'
import tailwindcss from 'tailwindcss'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import uni from '@dcloudio/vite-plugin-uni'

export default defineConfig(({ mode }) => {
	const systemEnv = loadEnv(mode, process.cwd(), ''); // 获取系统环境变量
	const customEnv = loadEnv(mode, './env', 'VITE_'); // 获取自定义环境变量
	console.log(customEnv)
	return {
		envDir: './env',
		resolve: {
			alias: {
			  // 设置路径
			  '~': './',
			  // 设置别名
			  '@': '@/'
			},
			// https://cn.vitejs.dev/config/#resolve-extensions
			extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
		},
		plugins: [
			uni(),
			AutoImport({
				imports:[
					// 预设
					'vue',
					'uni-app'
				]
			}),
			Components({
				resolvers: [],
			}),
		],
		// vite 相关配置
		server: {
		  port: 8080,
		  host: '0.0.0.0',
		  open: true,	
		  proxy: {
		    [customEnv.VITE_APP_BASE_API]: {
				target: customEnv.VITE_APP_BASE_URL,
				changeOrigin: true,
				rewrite: (path) => path.replace(new RegExp('^' + customEnv.VITE_APP_BASE_API), ''),
		    }
		  },
		  disableHostCheck: true
		},
		css: {
			postcss: {
				plugins: [
					tailwindcss(),
				],
			},
		},
	}
})